package com.tuyrt.hi.concurrent_demo

import android.os.Bundle
import android.util.Log
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.tuyrt.hi.concurrent_demo.coroutines.CoroutineScene2
import com.tuyrt.hi.concurrent_demo.coroutines.CoroutineScene2_decompiled
import com.tuyrt.hi.concurrent_demo.coroutines.CoroutineScene3
import com.tuyrt.hi.concurrent_demo.threadpool.HiExecutor
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.concurrent.thread
import kotlin.coroutines.Continuation
import kotlin.coroutines.suspendCoroutine

class MainActivity : AppCompatActivity() {

    private val TAG = "MainActivity"
    private var paused: Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // ThreadDemo.test()

        findViewById<Button>(R.id.button1).setOnClickListener {
            for (priority in 0 until 10) {
                HiExecutor.execute(priority) {
                    try {
                        Thread.sleep(1000L - priority * 100)
                    } catch (e: InterruptedException) {
                        e.printStackTrace()
                    }
                }
            }
        }

        findViewById<Button>(R.id.button2).setOnClickListener {
            if (paused) {
                HiExecutor.resume()
            } else {
                HiExecutor.pause()
            }
            paused = !paused
        }

        findViewById<Button>(R.id.button3).setOnClickListener {
            HiExecutor.execute(runnable = object : HiExecutor.Callable<String>() {

                override fun onBackground(): String {
                    Log.e(TAG, "onBackground-当前线程是: " + Thread.currentThread().name)
                    return "我是异步任务的结果"
                }

                override fun onCompleted(t: String) {
                    Log.e(TAG, "onCompleted-当前线程是: " + Thread.currentThread().name)
                    Log.e(TAG, "onCompleted-任务结果是result: $t")
                }
            })
        }

        findViewById<Button>(R.id.button4).setOnClickListener {
            lifecycleScope.launch {
                val string = CoroutineScene3.parseAssetsFile(assets, "readme.md")
                Log.e(TAG, "lifecycleScope string: $string")
            }
            Log.e(TAG, "lifecycleScope after click")
        }

        findViewById<Button>(R.id.button5).setOnClickListener {
//            lifecycleScope.launch {
//                CoroutineScene2.request2()
//            }

            val callback = Continuation<String>(Dispatchers.Main) { result ->
                Log.e(TAG, "CoroutineScene2_decompiled#result: ${result.getOrNull()}")
            }
            CoroutineScene2_decompiled.request1(callback)
        }

        findViewById<Button>(R.id.button6).setOnClickListener {
            homework()
            //javaHomework()
        }
    }

    fun javaHomework() {
        val threadA = MyThread("thread-a")
        val threadB = MyThread("thread-b")
        val threadC = MyThread("thread-c")
        threadA.start()
        threadA.join()
        threadB.start()
        threadB.join()
        threadC.start()
    }

    fun homework() {
        lifecycleScope.launch {

           /*val result1 =  suspendCoroutine<String>{ continuation ->
                thread {
                    println("corontine thread-a")
                    continuation.resumeWith(Result.success("thread-a"))
                }
            }
            val result2 =  suspendCoroutine<String>{ continuation ->
                thread {
                    println("corontine thread-b")
                    continuation.resumeWith(Result.success("thread-b"))
                }
            }
            val result3 =  suspendCoroutine<String>{ continuation ->
                thread {
                    println("corontine thread-c")
                    continuation.resumeWith(Result.success("thread-c"))
                }
            }*/
            startThreadA()
            startThreadB()
            startThreadC()
        }
    }


    suspend fun startThreadA() {
        thread {
            println("suspend startThreadA")
        }
    }
    suspend fun startThreadB() {
        delay(1000)
        thread {
            println("suspend startThreadB")
        }
    }
    suspend fun startThreadC() {
        delay(1000)
        thread {
            println("suspend startThreadC")
        }
    }

    class MyThread(name: String) : Thread(name) {
        override fun run() {
            println("Thread $name")

        }
    }

}